<template>
    <div style="height: 100%; position: relative">
        <el-drawer
            :title="type==='add'?'新增':'编辑'"
            :visible.sync="show_dialog"
            direction="rtl"
            size="80%"
            custom-class="demo-drawer"
            ref="drawer"
        >
            <div class="demo-drawer__content" ref="content">
                <el-form :model="form" :rules="rules" v-if="show_dialog" ref="editDrawer">
                    <el-form-item label="消息类型:" :label-width="formLabelWidth" prop="type">
                        <el-select v-model="form.type" filterable placeholder="请选择消息类型">
                            <el-option key="1" label="消息" value="1"></el-option>
                            <el-option key="2" label="公告" value="2"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="标题:" :label-width="formLabelWidth" prop="title">
                        <el-input v-model="form.title"></el-input>
                    </el-form-item>
                    <el-form-item label="消息内容:" :label-width="formLabelWidth" prop="content">
                        <tinymce
                            ref="tinymce"
                            v-model="form.content"
                            :height="300"
                            width="95%"
                            :cssObj="form.css"
                            :uploadData="uploadData"
                            :banUpload="true"
                        />
                    </el-form-item>
                </el-form>
            </div>
            <div class="demo-drawer__footer">
                <el-button @click="cancelForm">取 消</el-button>
                <el-button
                    type="primary"
                    @click="handleClose"
                    :loading="loading"
                    v-show="type!=='show'"
                >{{ loading ? "发送中 ..." : "发 送" }}</el-button>
            </div>
        </el-drawer>
    </div>
</template>
<script>
import Tinymce from '@/components/Tinymce'
import { dateFormatter } from "@/common/js/util"
export default {
    props: ['dialog'],
    components: { Tinymce },
    data() {
        return {
            loading: false,
            formLabelWidth: '180px',
            show_dialog: false,//弹窗显示
            form: {
                title: '',
                type: '',
                content: ''
            },
            rules: {
                type: [
                    { required: true, message: '请选择消息类型' }
                ],
                title: [
                    { required: true, message: '请输入消息标题' }
                ]
            },
            type: '',
            row: '',
            Services: '',//服务类型
            Scenarios: "",
            SubCateScenarios: "",
            supplierList: "",
            storesList: []
        }
    },
    created() {
        this.default_data();
    },
    methods: {
		uploadData(){},
        default_data() {
        },
        show_dialog_fn(type, row) {
            this.type = type;
            this.show_dialog = true;
            if (type === 'add') {
                this.form = {
                    title: '',
                    type: '',
                    content: ''
                };
            } else if (type === 'edit') {
                this.row = row;
                this.getItemDetail(row.noticeId)
            }
        },
        cancelForm() {
            this.loading = false
            this.show_dialog = false
            clearTimeout(this.timer)
        },
        handleClose() {
            let { title,
                type,
                content } = this.form;
            let findFn = (arr, key, value) => {
                return arr.find(item => item[key] == value)
            }
            let data = {
                title,
                type,
                content
            }
            this.$refs.editDrawer.validate(valid => {
                if (valid) {
                    if (this.type === 'edit') {
                        data.noticeId = this.row.noticeId;
                        this.editItemDetail(data);
                    } else if (this.type === 'add') {
                        this.addItemDetail(data);
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            })

        },
        getItemDetail(id) {
            this.$http.get(`/admin/im/index.php?act=notice&op=content&noticeId=${id}`).then(res => {
                if (res.code != 1) {
                    this.$message.warning(res.msg);
                    this.show_dialog = false;
                    return;
                }
                this.form = res.data;
            }).catch(err => {
                this._message('error', err);
            })
        },
        addItemDetail(data) {
            this.$http.post("/admin/im/index.php?act=notice&op=givePublicNotice", data).then(res => {
                if (res && res.code == 1) {
                    this.show_dialog = false;
                    this.$parent.getListData();
                    this.$message.success(res.msg);

                } else {
                    this.$message.warning(res.msg)
                }
            }).catch(err => {
                this._message('error', err);
            })
        },
        editItemDetail(data) {
            this.$http.post(`/admin/im/index.php?act=notice&op=update`, data).then(res => {
                if (res.code == 1) {
                    this.show_dialog = false;
                    this.$message.success(res.msg);
                    this.$parent.getListData()
                } else {
                    this.$message.warning(res.msg)
                }
            }).catch(err => {
                this._message('error', err);
            })
        }
    }
}
</script>

<style lang="scss" scoped>
.el-form {
    // width: 560px;
    padding-left: 10px;
}

.el-select /deep/.el-input {
    width: 360px;
}
.plantTable /deep/.el-input {
    width: auto;
}
form /deep/.el-input {
    width: 360px;
}

.demo-drawer__content {
    position: absolute;
    top: 0;
    bottom: 60px;
    width: 100%;
    overflow-y: auto;
}

/deep/.el-drawer__body {
    position: absolute;
    top: 77px;
    bottom: 0;
    width: 100%;
    overflow: auto;
    padding-bottom: 60px;
}

.demo-drawer__footer {
    position: absolute;
    bottom: 10px;
    left: 20px;
}

.avatar-uploader /deep/.el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader /deep/.el-upload:hover {
    border-color: #409eff;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
.row_line {
    border-bottom: 1px dashed #d7d8dc;
}
.goodsInfo {
    display: flex;
    align-items: center;
}
</style>
